//
//  UIView+LZYEmptyView.m
//  LZYEmptyViewDemo
//
//  Created by bjhj on 17/3/27.
//  Copyright © 2017年 刘占宇个人项目. All rights reserved.
//

#import "UIView+LZYEmptyView.h"
#import <objc/runtime.h>
#import "Masonry.h"

//屏幕高
#define LSCREENH [UIScreen mainScreen].bounds.size.height
///屏幕宽
#define LSCREENW [UIScreen mainScreen].bounds.size.width

@interface UIView ()

@property (nonatomic,copy) void(^reloadAction)();

@end

@implementation UIView (LZYEmptyView)

#pragma mark -- private method --
- (void)configReloadAction:(void (^)())block
{
    self.reloadAction = block;
    if (self.errorPageView && self.reloadAction) {
        self.errorPageView.didClickReloadBlock = self.reloadAction;
    }
    if (self.blankPageView && self.reloadAction) {
        self.blankPageView.didClickReloadBlock = self.reloadAction;
    }
}

//--reloadAction--
- (void)setReloadAction:(void (^)())reloadAction{
    objc_setAssociatedObject(self, @selector(reloadAction), reloadAction, OBJC_ASSOCIATION_COPY);
}
- (void (^)())reloadAction{
    return objc_getAssociatedObject(self, _cmd);
}

//--LZYErrorPageView--
- (void)setErrorPageView:(LZYErrorPageView *)errorPageView{
    //添加属性
    [self willChangeValueForKey:NSStringFromSelector(@selector(errorPageView))];
    objc_setAssociatedObject(self, @selector(errorPageView), errorPageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:NSStringFromSelector(@selector(errorPageView))];
}

- (LZYErrorPageView *)errorPageView{
    //获取属性
    return objc_getAssociatedObject(self, _cmd);
}

//--WNBlankPageView--
- (void)setBlankPageView:(LZYBlankPageView *)blankPageView{
    [self willChangeValueForKey:NSStringFromSelector(@selector(blankPageView))];
    objc_setAssociatedObject(self, @selector(blankPageView), blankPageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:NSStringFromSelector(@selector(blankPageView))];
}

- (LZYBlankPageView *)blankPageView{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)showErrorPageView{
    if (!self.errorPageView) {
        self.errorPageView = [[LZYErrorPageView alloc]initWithFrame:self.bounds];
        if (self.reloadAction) {
            self.errorPageView.didClickReloadBlock = self.reloadAction;
        }
    }
    [self addSubview:self.errorPageView];
    [self bringSubviewToFront:self.errorPageView];
}

- (NSInteger)showBlankPageViewWithCount:(NSArray *)datalist reloadAction:(nullable SEL)action WithTarget:(nullable id)target{
    if (datalist) {
        if (datalist.count > 0 && self.blankPageView){
            [self.blankPageView removeFromSuperview];
            self.blankPageView = nil;
        }else if (datalist.count == 0){
            if (!self.blankPageView) {
                self.blankPageView = [[LZYBlankPageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
                self.blankPageView.backgroundColor = [UIColor whiteColor];
                [self addSubview:self.blankPageView];
                [self bringSubviewToFront:self.blankPageView];
            }
            [self configReloadAction:^{
                [target performSelector:action];
            }];
        }
        return datalist.count;
    }else{
        return 0;
    }
}

- (NSInteger)showBlankPageViewWithCount:(NSArray *)datalist{
    if (datalist) {
        if (datalist.count > 0 && self.blankPageView){
            [self.blankPageView removeFromSuperview];
            self.blankPageView = nil;
        }else if (datalist.count == 0){
            if (!self.blankPageView) {
                self.blankPageView = [[LZYBlankPageView alloc]initWithFrame:self.bounds];
                self.blankPageView.backgroundColor = [UIColor whiteColor];
            }
            [self addSubview:self.blankPageView];
            [self bringSubviewToFront:self.blankPageView];
        }
        return datalist.count;
    }else{
        return 0;
    }
}

- (void)hideErrorPageView
{
    if (self.errorPageView) {
        [self.errorPageView removeFromSuperview];
        self.errorPageView = nil;
    }
}

- (void)hideBlankPageView{
    if (self.blankPageView) {
        [self.blankPageView removeFromSuperview];
        self.blankPageView = nil;
    }
}

@end

#pragma mark --- LZYErrorPageView

@interface LZYErrorPageView ()

@property (nonatomic,weak) UIImageView *errorImageView;
@property (nonatomic,weak) UILabel *errorTipLabel;
@property (nonatomic,weak) UIButton *reloadButton;


@end


@implementation LZYErrorPageView
@synthesize didClickReloadBlock=_didClickReloadBlock;

- (instancetype)init{
    return  [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIImageView *errorImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LZYErrorPage"]];
        _errorImageView = errorImageView;
        [self addSubview:_errorImageView];
    
        UILabel *errorTipLabel = [[UILabel alloc]init];
        errorTipLabel.numberOfLines = 1;
        errorTipLabel.font = [UIFont systemFontOfSize:16];
        errorTipLabel.textAlignment = NSTextAlignmentCenter;
        errorTipLabel.textColor = [UIColor darkGrayColor];
        errorTipLabel.text = @"连接失败或找不到链接";
        _errorTipLabel = errorTipLabel;
        [self addSubview:_errorTipLabel];
        
//        UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        reloadButton.layer.masksToBounds = YES;
//        reloadButton.layer.cornerRadius = 5;
//        [reloadButton setTitle:@"点击刷新" forState:UIControlStateNormal];
//        reloadButton.titleLabel.font =[UIFont systemFontOfSize:15];
//        [reloadButton setImage:[UIImage imageNamed:@"DWQrefresh_white" ] forState:UIControlStateNormal];
//        reloadButton.backgroundColor = MainColor;
//        [reloadButton addTarget:self action:@selector(_clickReloadButton:) forControlEvents:UIControlEventTouchUpInside];
//        _reloadButton = reloadButton;
//        [self addSubview:_reloadButton];
        
        [_errorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self.mas_centerY).offset(-30);
        }];
        
        [_errorTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@20);
            make.top.equalTo(_errorImageView.mas_bottom).offset(10);
        }];
        
        [_reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(30);
            make.centerX.equalTo(self);
            make.bottom.equalTo(_errorTipLabel.mas_bottom).offset(50);
        }];
    }
    return self;
}



- (void)_clickReloadButton:(UIButton *)btn{
    if (_didClickReloadBlock) {
        _didClickReloadBlock();
    }
}

- (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
{
    [self _clickReloadButton:nil];
}

@end


#pragma mark --- LZYBlankPageView
@interface LZYBlankPageView ()
@property (nonatomic,weak) UIImageView* nodataImageView;
@property (nonatomic,weak) UILabel* nodataTipLabel;
@property (nonatomic,weak) UIButton *reloadButton;
@end

@implementation LZYBlankPageView
@synthesize didClickReloadBlock=_didClickReloadBlock;
- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView* nodataImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LZYBlankPage"]];
        _nodataImageView = nodataImageView;
        [self addSubview:_nodataImageView];
        
        UILabel* nodataTipLabel = [[UILabel alloc]init];
        nodataTipLabel.numberOfLines = 1;
        nodataTipLabel.font = [UIFont boldSystemFontOfSize:17];
        nodataTipLabel.textAlignment = NSTextAlignmentCenter;
        nodataTipLabel.textColor = [UIColor grayColor];
        nodataTipLabel.text = @"暂无数据";
        _nodataTipLabel = nodataTipLabel;
        [self addSubview:_nodataTipLabel];
        
        UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        reloadButton.layer.masksToBounds = YES;
        reloadButton.layer.cornerRadius = 5;
        [reloadButton setTitle:@"点击刷新" forState:UIControlStateNormal];
        reloadButton.titleLabel.font =[UIFont systemFontOfSize:15];
        [reloadButton setImage:[UIImage imageNamed:@"DWQrefresh_white" ] forState:UIControlStateNormal];
        reloadButton.backgroundColor = MainColor;
        [reloadButton addTarget:self action:@selector(_clickReloadButton:) forControlEvents:UIControlEventTouchUpInside];
        _reloadButton = reloadButton;
        [self addSubview:_reloadButton];
        
        [_nodataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(120);
            make.bottom.equalTo(self.mas_centerY).offset(-10);
        }];
        
        [_nodataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@50);
            make.top.equalTo(_nodataImageView.mas_bottom).offset(5);
        }];
        
        [_reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(30);
            make.centerX.equalTo(self);
            make.bottom.equalTo(_nodataTipLabel.mas_bottom).offset(50);
        }];
    }
    return self;
}


- (void)_clickReloadButton:(UIButton *)btn{
    if (_didClickReloadBlock) {
        _didClickReloadBlock();
    }
}

- (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
{
    [self _clickReloadButton:nil];
}

@end










